package com.lgx.mall.business.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.web.Pagination;
import com.lgx.mall.business.dao.IReturnRequestDao;
import com.lgx.mall.business.entity.ReturnRequest;

@Component
public class ReturnRequestDao extends GeneralDao implements IReturnRequestDao{
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;

	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	/**
	 * 创建记录
	 */
	@Override
	public ReturnRequest createReturnRequest(ReturnRequest returnRequest) {
		this.getHibernateTemplate().persist(returnRequest);
		this.getHibernateTemplate().flush();
		log.info("[创建订单]:"+returnRequest.getId());
		return returnRequest;
	}

	/**
	 * 删除记录
	 */
	@Override
	public void deleteReturnRequest(ReturnRequest returnRequest) {
		this.getHibernateTemplate().delete(returnRequest);
		log.info("[删除订单]:"+returnRequest.getId());
		this.getHibernateTemplate().flush();
	}

	/**
	 * 修改记录
	 */
	@Override
	public void updateReturnRequest(ReturnRequest returnRequest) {
		log.info("[修改订单]:"+returnRequest.getId());
		this.getHibernateTemplate().update(returnRequest);
		this.getHibernateTemplate().flush();
	}

	/**
	 * 查询全部记录
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ReturnRequest> findReturnRequest() {
		String sql = "from ReturnRequest";
		return this.getHibernateTemplate().find(sql);
	}

	/**
	 * 根据id查询记录
	 */
	@Override
	public ReturnRequest findReturnRequest(Long id) {
		return this.getHibernateTemplate().get(ReturnRequest.class, id);
	}

	/**
	 * 分页查询
	 */
	@Override
	public Pagination findReturnPagination(Long orderId, int pageSize,
			int pageNum) {
		String sql = "from ReturnRequest r where r.order.id ="+orderId;
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	/**
	 * 根据订单号检索
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ReturnRequest> findReturnRequests(Long orderId) {
		String sql = "from ReturnRequest r where r.order.id ="+orderId;
		return this.getHibernateTemplate().find(sql);
	}

}
